package com.stone.bigdata.config;

import com.stone.bigdata.cron.AfterFiveMinAdd;
import com.stone.bigdata.cron.CleanCache;
import com.stone.bigdata.cron.CountActiveCity;
import com.stone.bigdata.cron.CountAddTotal;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartConfig {

    @Bean
    public JobDetail cleanCacheJob() {
        JobDetail jobDetail = JobBuilder.newJob(CleanCache.class)
                .withIdentity("clean cache", "redis job")
                .storeDurably()
                .build();
        return jobDetail;
    }

    @Bean
    public Trigger cleanCacheTrigger() {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(cleanCacheJob())
                .withIdentity("clean cache", "redis trigger")
                .startNow()
                // 每天00:01点执行
                .withSchedule(CronScheduleBuilder.cronSchedule("0 1 0 * * ?"))
                .build();
        return trigger;
    }

    @Bean
    public JobDetail countActiveCityJob() {
        JobDetail jobDetail = JobBuilder.newJob(CountActiveCity.class)
                .withIdentity("count active city", "save job")
                .storeDurably()
                .build();
        return jobDetail;
    }

    @Bean
    public Trigger countActiveCityTrigger() {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(countActiveCityJob())
                .withIdentity("count active city", "save trigger")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 1 0 * * ?"))
                .build();
        return trigger;
    }

    @Bean
    public JobDetail countAddTotalJob() {
        JobDetail jobDetail = JobBuilder.newJob(CountAddTotal.class)
                .withIdentity("count add total", "save job 1")
                .storeDurably()
                .build();
        return jobDetail;
    }

    @Bean
    public Trigger countAddTotalTrigger() {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(countAddTotalJob())
                .withIdentity("count add total", "save trigger 1")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 5 11 * * ?"))
                .build();
        return trigger;
    }

    @Bean
    public JobDetail countFiveAfterJob() {
        JobDetail jobDetail = JobBuilder.newJob(AfterFiveMinAdd.class)
                .withIdentity("count five after", "save job 2")
                .storeDurably()
                .build();
        return jobDetail;
    }

    @Bean
    public Trigger countFiveAfterTrigger() {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(countFiveAfterJob())
                .withIdentity("count five after", "save trigger 2")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0/5 * * * ?"))
                .build();
        return trigger;
    }
}
